package gui;
import javax.swing.JPanel;
	import java.awt.Frame;
	import javax.swing.JDialog;
	import javax.swing.JLabel;
	import java.awt.Rectangle;
	import java.awt.Font;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
	import javax.swing.JOptionPane;
	import javax.swing.JTextField;
	import javax.swing.JButton;

	import negocios.Maquinas;
	import negocios.Fachada;
import javax.swing.WindowConstants;
import javax.swing.text.MaskFormatter;

	public class CadastroDeMaquinas extends JDialog{
	
		private static final long serialVersionUID = 1L;
		private JPanel jContentPane = null;
		private JLabel lTitulo = null;
		private JLabel lGrupo = null;
		private JFormattedTextField tGrupo = null;
		private JLabel lDescricao = null;
		private JFormattedTextField tDescricao = null;		
		private JButton bConfirmar = null;
		private JButton bLimpar = null;
		private JButton bCancelar = null;
		private JFormattedTextField tMarca = null;
		private JLabel lMarca = null;

	
		public CadastroDeMaquinas(Frame owner) {
			super(owner);
			initialize();
		}

		private void initialize() {
			this.setSize(475, 225);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setModal(true);
			this.setLocationRelativeTo(null);
			this.setContentPane(getJContentPane());
		}


		private JPanel getJContentPane() {
			if (jContentPane == null) {
				lMarca = new JLabel();
				lMarca.setBounds(new Rectangle(87, 58, 48, 16));
				lMarca.setText("Marca:");
				lDescricao = new JLabel();
				lDescricao.setBounds(new Rectangle(87, 111, 64, 16));
				lDescricao.setText("Descricao: ");
				lGrupo = new JLabel();
				lGrupo.setBounds(new Rectangle(87, 82, 43, 16));
				lGrupo.setText("Grupo: ");
				lTitulo = new JLabel();
				lTitulo.setBounds(new Rectangle(115, 20, 288, 23));
				lTitulo.setText("Cadastro de Maquinas");
				lTitulo.setFont(new Font("Tahoma", Font.BOLD, 25));
				jContentPane = new JPanel();
				jContentPane.setLayout(null);
				jContentPane.add(lTitulo, null);
				jContentPane.add(lGrupo, null);
				jContentPane.add(getTGrupo(), null);
				jContentPane.add(lDescricao, null);
				jContentPane.add(getTDescricao(), null);
				jContentPane.add(getBConfirmar(), null);
				jContentPane.add(getBLimpar(), null);
				jContentPane.add(getBCancelar(), null);
				jContentPane.add(getTMarca(), null);
				jContentPane.add(lMarca, null);
			}
			return jContentPane;
		}


		private JFormattedTextField getTGrupo() {
			if (tGrupo == null) {
				tGrupo = new JFormattedTextField();
				tGrupo.setBounds(new Rectangle(168, 82, 173, 20));
			}
			return tGrupo;
		}

		
		private JTextField getTDescricao() {
			if (tDescricao == null) {	
				tDescricao = new JFormattedTextField();
				tDescricao.setBounds(new Rectangle(168, 110, 173, 20));
			}
			return tDescricao;
		}


		private JButton getBConfirmar() {
			if (bConfirmar == null) {
				bConfirmar = new JButton();
				bConfirmar.setBounds(new Rectangle(75, 161, 93, 20));
				bConfirmar.setText("Confirmar");
				bConfirmar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						int grupo = Integer.parseInt(tGrupo.getText());
						String desc = tDescricao.getText();
						String marca = tMarca.getText();
						Maquinas maquina = new Maquinas();
						maquina.setGrupo(grupo);
						maquina.setDescricao(desc);
						maquina.setMarca(marca);
						try {
							if ((tGrupo.getText().equals("")) || (tDescricao.getText().equals(""))||(tMarca.getText().equals(""))){
								JOptionPane.showMessageDialog(null,"Por favor, preencha todos os campos!","Sig Confex - Erro!",	JOptionPane.INFORMATION_MESSAGE,null);
								tGrupo.requestFocus();							
							}else{
								
								Maquinas x=null;
								
								//Teste de inser��o dupicada
								
								x = Fachada.getInstancia().ConsultarMaquinas(maquina,true);
								if (x == null){
								
									Fachada.getInstancia().IncluiMaquinas(maquina);
									JOptionPane.showMessageDialog(null,"Maquina inserida com sucesso.","Confirmacao de inclusao",JOptionPane.INFORMATION_MESSAGE,null);
							
								}else JOptionPane.showMessageDialog(null,"Maquina ja existente.","Confirmacao de inclusao",JOptionPane.ERROR_MESSAGE,null);
								   
								
							}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,"Maquina nao inserido.","Confirmacao de inclusao",JOptionPane.ERROR_MESSAGE,null);
					e1.printStackTrace();
				}
						tGrupo.setText("");
						tDescricao.setText("");
						tMarca.setText("");
						tGrupo.requestFocus();
			}
		});
			}
			return bConfirmar;
		}

		
		private JButton getBLimpar() {
			if (bLimpar == null) {
				bLimpar = new JButton();
				bLimpar.setBounds(new Rectangle(176, 161, 93, 20));
				bLimpar.setText("Limpar");
				bLimpar.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						tGrupo.setText("");
						tDescricao.setText("");
						tMarca.setText("");
						tGrupo.requestFocus();
					}
			});

			}
			return bLimpar;
		}

	
		private JButton getBCancelar() {
			if (bCancelar == null) {
				bCancelar = new JButton();
				bCancelar.setBounds(new Rectangle(278, 162, 93, 20));
				bCancelar.setText("Cancelar");
				bCancelar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						setVisible(false);
						dispose();
					}
				});
			}
			return bCancelar;
		}

		/**
		 * This method initializes tMarca	
		 * 	
		 * @return javax.swing.JTextField	
		 */
		private JTextField getTMarca() {
			if (tMarca == null) {
				tMarca = new JFormattedTextField();
				tMarca.setBounds(new Rectangle(168, 50, 173, 20));
			}
			return tMarca;
		}

		
	}

